/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.supinfo.youfood.web.servlet.cuisine;

import com.supinfo.youfood.entity.Commande;
import com.supinfo.youfood.entity.Commande_Menu;
import com.supinfo.youfood.entity.Commande_Produit;
import com.supinfo.youfood.service.Commande_MenuService;
import com.supinfo.youfood.service.Commande_ProduitService;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Michael
 */
@WebServlet(name = "setCommandeEtatServlet", urlPatterns = {"/setCommandeEtat"})
public class setCommandeEtatServlet extends HttpServlet {
    
    @EJB
    private Commande_MenuService commande_menuService;
    
    @EJB
    private Commande_ProduitService commande_produitService;

    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    }

    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String type = request.getParameter("type");
        if(type.equals("menu")) {
            Long id_commande_menu = Long.valueOf(request.getParameter("id_commande_menu").trim());
            int etat = Integer.valueOf(request.getParameter("etat"));
            Commande_Menu currentCommande_Menu = commande_menuService.findCommande_MenuById(id_commande_menu);
            currentCommande_Menu.setEtat(etat);
            commande_menuService.updateCommande_Menu(currentCommande_Menu);
            
            //Envoi réponse au navigateur
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print("OK");
        }
        else if(type.equals("produit")) {
            Long id_commande_produit = Long.valueOf(request.getParameter("id_commande_produit").trim());
            int etat = Integer.valueOf(request.getParameter("etat"));
            Commande_Produit currentCommande_Produit = commande_produitService.findCommande_ProduitById(id_commande_produit);
            currentCommande_Produit.setEtat(etat);
            commande_produitService.updateCommmande_Produit(currentCommande_Produit);
            
            //Envoi réponse au navigateur
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print("OK");
        }
    }
}
